//
//  AppDelegate.m
//  VoIPTest
//
//  Created by Tg W on 17/2/21.
//  Copyright © 2017年 oopsr. All rights reserved.
//

#import "AppDelegate.h"
#import <PushKit/PushKit.h>
#import <JitsiMeetSDK/JitsiMeetSDK-Swift.h>
#import <JitsiMeetSDK/JitsiMeet.h>
#import "ViewController.h"
#import "JoinMeetViewController.h"
#define SCREEN_RECT ([UIScreen mainScreen].bounds)
@interface AppDelegate ()<PKPushRegistryDelegate>{
    
    NSString *token;
}

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    self.pp = [[ProviderDelegateTest alloc]initWithCallManager:[[CallManager alloc]init]];
    
  
    
    PKPushRegistry *pushRegistry = [[PKPushRegistry alloc] initWithQueue:dispatch_get_main_queue()];
    pushRegistry.delegate = self;
    pushRegistry.desiredPushTypes = [NSSet setWithObject:PKPushTypeVoIP];
    
    [self configureJitsiMeet];
    
    ViewController *vc = [[ViewController alloc]init];
    self.window = [[UIWindow alloc] initWithFrame:SCREEN_RECT];
    self.window.backgroundColor = UIColor.whiteColor;
    self.window.rootViewController = vc;
    [self.window makeKeyAndVisible];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(meetingNewInfo:) name:@"klk" object:nil];
    
    return YES;
}

- (void)meetingNewInfo:(NSNotification *)notification {

    JoinMeetViewController *vc = [[JoinMeetViewController alloc]init];
    self.window.rootViewController = vc;
        
}

-(void)configureJitsiMeet{
    JitsiMeetConferenceOptions *defaultOptions = [JitsiMeetConferenceOptions fromBuilder:^(JitsiMeetConferenceOptionsBuilder * builder) {
        builder.serverURL = [NSURL URLWithString:@"https://v.gooday.live/"];
        [builder setFeatureFlag:@"help.enabled" withBoolean:NO];
        [builder setFeatureFlag:@"add-people.enabled" withBoolean:NO];
        [builder setFeatureFlag:@"calendar.enabled" withBoolean:NO];
        [builder setFeatureFlag:@"close-captions.enabled" withBoolean:NO];
        [builder setFeatureFlag:@"chat.enabled" withBoolean:NO];
        [builder setFeatureFlag:@"invite.enabled" withBoolean:NO];
        [builder setFeatureFlag:@"live-streaming.enabled" withBoolean:NO];
        [builder setFeatureFlag:@"kick-out.enabled" withBoolean:NO];
        [builder setFeatureFlag:@"security-options.enabled" withBoolean:NO];
        [builder setFeatureFlag:@"meeting-name.enabled" withBoolean:NO];
        [builder setFeatureFlag:@"meeting-password.enabled" withBoolean:NO];
        [builder setFeatureFlag:@"audio-focus.enabled" withBoolean:NO];
        [builder setFeatureFlag:@"notifications.enabled" withBoolean:NO];
        [builder setFeatureFlag:@"video-share.enabled" withBoolean:NO];
        [builder setFeatureFlag:@"conference-timer.enabled" withBoolean:NO];
        
        [builder setFeatureFlag:@"call-integration.enabled" withBoolean:NO];
        [builder setFeatureFlag:@"pip.enabled" withBoolean:NO];
        [builder setFeatureFlag:@"raise-hand.enabled" withBoolean:NO];
        [builder setFeatureFlag:@"toolbox.alwaysVisible" withBoolean:NO];
        
    }];
    
    [JitsiMeet sharedInstance].defaultConferenceOptions = defaultOptions;
    
}
#pragma mark -pushkitDelegate

- (void)pushRegistry:(PKPushRegistry *)registry didUpdatePushCredentials:(PKPushCredentials *)credentials forType:(NSString *)type{
    
    NSLog(@"%@",credentials.token);

    //应用启动获取token，并上传服务器
    token = [[[[credentials.token description] stringByReplacingOccurrencesOfString:@"<"withString:@""]
                    stringByReplacingOccurrencesOfString:@">" withString:@""]
                   stringByReplacingOccurrencesOfString:@" " withString:@""];
    
    NSLog(@"%@",token);
}

- (void)pushRegistry:(PKPushRegistry *)registry didReceiveIncomingPushWithPayload:(PKPushPayload *)payload forType:(NSString *)type{
    
    [self.pp reportIncomingCall:[NSUUID UUID] andHandle:@"11111" andHasvideo:NO andCompletion:^(NSError * _Nonnull error) {
        
    }];
    
//    UIBackgroundTaskIdentifier aa = [UIApplication.sharedApplication beginBackgroundTaskWithExpirationHandler:^{
//
//    }];
//
//    [self.providerDelegate reportIncomingCall:[NSUUID UUID] handle:@"111" displayName:@"222" completion:^(NSError * _Nullable error) {
////        [[UIApplication sharedApplication] UIEventSubtypeRemoteControlEndSeekingBackward]
////                    [UIApplication.sharedApplication endBackgroundTask:aa];
//    }];
    
}

//- (UISceneConfiguration *)application:(UIApplication *)application configurationForConnectingSceneSession:(UISceneSession *)connectingSceneSession options:(UISceneConnectionOptions *)options API_AVAILABLE(ios(13.0)){
//    return [UISceneConfiguration configurationWithName:@"Default Configuration" sessionRole:connectingSceneSession.role];
//}

@end
